<?php
namespace app\dboard\controller;
use think\Controller;
use think\Db;
use think\Request;
class Tqr extends Controller
{
    public $return_data=['Code'=>1,'Msg'=>'','ErrorSql'=>'','Data'=>[]];
    public function store($sid)
    {
        $sid = isset($sid)?intval($sid):0;
        if(!$sid){
            $this->return_data['Msg'] = "缺少参数";
            return $this->print_result($this->return_data);
        } 
        $where[] =['id','=',$sid];
        $store =Db::name('store')->where($where)->find();
        if (!$store){
            $this->return_data['Msg'] = "无此商品";
            return $this->print_result($this->return_data);
        }
        if ($store['wxcode']){
            $c['cover'] = get_basecover_img($store['wxcode']);
            $c['path'] = $store['wxcode'];
            $this->return_data['Code']=0;
            $this->return_data['Msg'] = "已经抓取过小程序码";
            $this->return_data['Data']=$c;// 商品封面
            return $this->print_result($this->return_data);
        }
        $appid = config('appid');
        $appsecret = config('appsecret');
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        $at =file_get_contents($url);
        $at = json_decode($at,true);
        $qrurl = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$at['access_token'];
        $qrdata['path'] = 'pages/index/Index?sid='.$sid;
        $qrdata['is_hyaline'] = true;
        $qrinfo = curl_data($qrurl,json_encode($qrdata));
        $filename='store_wxcode_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!file_put_contents($file,$qrinfo)){
            $this->return_data['Msg']="上传错误";
            return $this->print_result($this->return_data);
        }
        $c['cover'] = get_basecover_img($filepath);
        $c['path'] = $filepath;

        $data['wxcode'] = $filepath;
        $data['updatetime'] = time();
        $update = Db::name('store')->where('id',$sid)->update($data);
        if (!$update) {
          $this->return_data['Msg'] = "失败";
          return $this->print_result($this->return_data);
        }
        $this->return_data['Code']=0;
        $this->return_data['Msg']='成功';
        $this->return_data['Data']=$c;// 商品封面
        return $this->print_result($this->return_data);
    }
    public function teacher($tid)
    {
        $tid = isset($tid)?intval($tid):0;
        if(!$tid){
            $this->return_data['Msg'] = "缺少参数";
            return $this->print_result($this->return_data);
        } 
        $where[] =['id','=',$tid];
        $teacher=Db::name('teacher')->where($where)->find();
        if (!$teacher){
            $this->return_data['Msg'] = "无此老师";
            return $this->print_result($this->return_data);
        }
        if ($teacher['wxcode']){
            $c['cover'] = get_basecover_img($teacher['wxcode']);
            $c['path'] = $teacher['wxcode'];
            $this->return_data['Code']=0;
            $this->return_data['Msg'] = "已经抓取过小程序码";
            $this->return_data['Data']=$c;// 商品封面
            return $this->print_result($this->return_data);
        }
        $appid = config('appid');
        $appsecret = config('appsecret');
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        $at =file_get_contents($url);
        $at = json_decode($at,true);
        $qrurl = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$at['access_token'];
        $qrdata['path'] = 'pages/card/VisitingCard?tid='.$tid;
        $qrinfo = curl_data($qrurl,json_encode($qrdata));
        $filename='teacher_wxcode_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!file_put_contents($file,$qrinfo)){
            $this->return_data['Msg']="上传错误";
            return $this->print_result($this->return_data);
        }
        $c['cover'] = get_basecover_img($filepath);
        $c['path'] = $filepath;
        $data['wxcode'] = $filepath;
        $data['updatetime'] = time();
        $update = Db::name('teacher')->where('id',$tid)->update($data);
        if (!$update) {
          $this->return_data['Msg'] = "失败";
          return $this->print_result($this->return_data);
        }
        $this->return_data['Code']=0;
        $this->return_data['Msg']='成功';
        $this->return_data['Data']=$c;// 商品封面
        return $this->print_result($this->return_data);
    }
    public function print_result($return_data){
        if(!is_array($return_data)){
            $return_data['Code']=0;
            $return_data['Msg']='输出信息不符合规范';
        }
        return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
    }
//https://v.qq.com/x/page/c0799d0jruj.html 
    public function pr(){
        $return_data = getVideoInfo('https://v.qq.com/x/page/v30303cktcs.html');
        echo $return_data;exit;
        return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
    }



    public function test()
    {
        $template_id = 'NfBxbtgH8eYL7liXJDRmRgmNAaT8FbbQooCgkYsjYoQ';
        $page = 'orderPackages/pages/admin/leave?notice=1&sid=33';
        $openid = 'oHqXG58xi_zII0jmFRmrv2KBXRuk';
        $data = [
            'first' => ['value' => '青菜老师老师，你有一条学员请假通知"'],
            'keyword1' => ['value' => '潘峰'],
            'keyword2' => ['value' => '2021-01-27 09:00'],
            'keyword3' => ['value' => '珠心算高级课程'],
            'keyword4' => ['value' => '家中有事不能来上课x'],
            'remark' => ['value' => '点击详情，进入请假管理页面'],
        ];
        $PublicsLogic = new \app\api\logic\Publics();
        $res = $PublicsLogic->send_template_message($template_id, $page, $data, $openid);
        var_dump($res); 
    }
}
